/*
* pps_bom
*/
package com.pitop.assembly.service;

import com.pitop.assembly.api.AssemblyCategoryFeignApi;
import com.pitop.assembly.api.AssemblyFeignApi;
import com.pitop.assembly.entity.Assembly;
import com.pitop.assembly.entity.AssemblyCategory;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.AssemblyException;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 组件
 *
 * @author berton
 */
@Service("assemblySVImpl")
@Slf4j
public class AssemblySVImpl {

    @Autowired
    private AssemblyFeignApi assemblyFeignApi;

    @Autowired
    private AssemblyCategoryFeignApi assemblyCategoryFeignApi;

    /**
     * 加载一个对象Assembly
     *
     * @param id * @param code 编码
     * @return Assembly
     */
    public Assembly load(Long id, String code) {
        if (id == null && code == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyFeignApi.load(id, code);
    }

    /**
     * 加载一个对象Assembly 通过id,(将查询关联数据)
     *
     * @param id
     * @return Assembly
     */
    public Assembly loadById(Long id) {
        if (id == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyFeignApi.loadById(id);
    }

    /**
     * 加载一个对象Assembly 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return Assembly
     */
    public Assembly loadByCode(String code) {
        if (code == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyFeignApi.loadByCode(code);
    }

    /**
     * 加载一个对象Assembly,(将查询关联数据)
     *
     * @param id * @param code 编码
     * @return Assembly
     */
    public Assembly get(Long id, String code) {
        if (id == null && code == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyFeignApi.get(id, code);
    }

    /**
     * 加载一个对象Assembly 通过id,(将查询关联数据)
     *
     * @param id
     * @return Assembly
     */
    public Assembly getById(Long id) {
        if (id == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyFeignApi.getById(id);
    }

    /**
     * 加载一个对象Assembly 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return Assembly
     */
    public Assembly getByCode(String code) {
        if (code == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyFeignApi.getByCode(code);
    }

    /**
     * 删除对象Assembly
     *
     * @param id * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        assemblyFeignApi.delete(id, code);
    }

    /**
     * 查询Assembly分页
     *
     * @param assembly 对象
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Assembly>
     */
    public List<Assembly> list(Assembly assembly, int curPage, int pageSize) {
        return assemblyFeignApi.list(assembly, curPage, pageSize);
    }

    /**
     * 查询Assembly分页
     *
     * @param bomCode  bom编码
     * @param name     组件名称
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Assembly>
     */
    public List<Assembly> listForBom(String bomCode, String name, int curPage, int pageSize) {
        return assemblyFeignApi.listForBom(bomCode, name, curPage, pageSize);
    }

    /**
     * 查询Assembly分页
     *
     * @param id       * @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Assembly>
     */
    public List<Assembly> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        Assembly assembly = new Assembly();
        assembly.setId(id);
        assembly.setCode(code);

        return assemblyFeignApi.list(assembly, curPage, pageSize);
    }

    public int count(Long id, String code) {
        return assemblyFeignApi.count(id, code);
    }

    public int count(Assembly assembly) {
        return assemblyFeignApi.count(assembly);
    }

    /**
     * 根据bom编码查询组件信息
     *
     * @param bomCode bom编码
     * @param name    组件名称
     * @return int
     */
    public int countForBom(String bomCode, String name) {
        return assemblyFeignApi.countForBom(bomCode, name);
    }

    /**
     * 保存
     * 1.参数合法性验证
     * 2.查询名称是否已经被使用
     * 3.组件分类是否存在
     * 4.上级组件是否存在
     *
     * @param assembly 实体
     * @param account
     * @param code     @throws BaseException
     */
    public void save(Assembly assembly, String account, String code) {
        //1.参数合法性验证
        if (assembly == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //2.查询名称是否已经被使用
        Assembly assemblyFlag = assemblyFeignApi.loadByName(assembly.getName());
        if (assemblyFlag != null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Assembly_Exist);
        }
        if (StringTools.isNotEmpty(assembly.getAssemblyCode())) {
            assemblyFlag = assemblyFeignApi.loadByAssemblyCode(assembly.getAssemblyCode());
            if (assemblyFlag != null) {
                throw new AssemblyException(BaseException.BaseExceptionEnum.Assembly_Exist);
            }
        }
        //3.组件分类是否存在
        AssemblyCategory assemblyCategory = assemblyCategoryFeignApi.loadByCode(assembly.getCategoryCode());
        if (assemblyCategory == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Assembly_Category_Not_Exist);
        }
        //4.上级组件是否存在
        if (StringTools.isNotEmpty(assembly.getPreCode())) {
            Assembly fatherAssembly = assemblyFeignApi.loadByCode(assembly.getPreCode());
            if (fatherAssembly == null) {
                throw new AssemblyException(BaseException.BaseExceptionEnum.Father_Assembly_Not_Exist);
            }
        }
        assemblyFeignApi.build(assembly, account, code);
    }

    /**
     * 更新
     *
     * @param assembly    实体
     * @param account
     * @param accountCode @throws BaseException
     */
    public void modify(Assembly assembly, String account, String accountCode) {
        if (assembly == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //2.查询名称是否已经被使用
        Assembly assemblyFlag = assemblyFeignApi.loadByName(assembly.getName());
        if (assemblyFlag != null && !assembly.getCode().equals(assemblyFlag.getCode())) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Assembly_Exist);
        }
        //2.查询名称是否已经被使用
        if (StringTools.isNotEmpty(assembly.getAssemblyCode())) {
            assemblyFlag = assemblyFeignApi.loadByAssemblyCode(assembly.getAssemblyCode());
            if (assemblyFlag != null && !assembly.getCode().equals(assemblyFlag.getCode())) {
                throw new AssemblyException(BaseException.BaseExceptionEnum.Assembly_Exist);
            }
        }
        //3.组件分类是否存在
        AssemblyCategory assemblyCategory = assemblyCategoryFeignApi.loadByCode(assembly.getCategoryCode());
        if (assemblyCategory == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Assembly_Category_Not_Exist);
        }
        //4.上级组件是否存在
        if (StringTools.isNotEmpty(assembly.getPreCode())) {
            Assembly fatherAssembly = assemblyFeignApi.loadByCode(assembly.getPreCode());
            if (fatherAssembly == null) {
                throw new AssemblyException(BaseException.BaseExceptionEnum.Father_Assembly_Not_Exist);
            }
        }
        //5.修改
        assemblyFeignApi.modify(assembly, account, accountCode);
    }

    /**
     * 添加组件关联物料
     *
     * @param code          实体
     * @param materialCodes 实体
     * @throws BaseException
     */
    public void addMaterial(String code, String materialCodes) {
        if (StringTools.isEmpty(code) || StringTools.isEmpty(materialCodes)) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        assemblyFeignApi.addMaterial(code, materialCodes);
    }

    /**
     * 复制组件
     *
     * @param code        实体
     * @param account
     * @param accountCode @throws BaseException
     */
    public void copy(String code, String account, String accountCode) {
        if (StringTools.isEmpty(code)) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Assembly assembly = assemblyFeignApi.loadByCode(code);
        if (assembly == null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        String name = assembly.getName() + "_copy";
        Assembly assemblyFlag = assemblyFeignApi.loadByName(name);
        if (assemblyFlag != null) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Assembly_Exist);
        }
        assemblyFeignApi.copy(code, account, accountCode);
    }

    /**
     * 添加组件状态
     *
     * @param code  实体
     * @param state 实体
     * @throws BaseException
     */
    public Assembly updateState(String code, String state) {
        if (StringTools.isEmpty(code) || StringTools.isEmpty(state)) {
            throw new AssemblyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyFeignApi.updateState(code, state);
    }
}
